package com.hdjk.view.activity.personalcenter;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.dialog.EditTextDialog;
import com.hdjk.view.entity.UserEntity;
import com.hdjk.view.network.PersonalNetWork;
import com.hdjk.view.util.ToastUtils;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by sxy on 2017/11/10.
 */

public class PersonalInfoActivity extends BaseActivity implements View.OnClickListener {
    private UserEntity mUser;

    // Content View Elements
    private com.lst.projectlib.component.CustomView.TopTitleBar ttb_title_bar;
    private RelativeLayout rel_name;
    private ImageView iv_name;
    private TextView tv_name;
    private RelativeLayout rel_id;
    private ImageView iv_id;
    private TextView tv_id;
    private RelativeLayout rel_phone;
    private ImageView iv_phone;
    private TextView tv_phone;
    // End Of Content View Elements

    private void bindViews() {
        ttb_title_bar = (com.lst.projectlib.component.CustomView.TopTitleBar) findViewById(R.id.ttb_title_bar);
        rel_name = (RelativeLayout) findViewById(R.id.rel_name);
        iv_name = (ImageView) findViewById(R.id.iv_name);
        tv_name = (TextView) findViewById(R.id.tv_name);
        rel_id = (RelativeLayout) findViewById(R.id.rel_id);
        iv_id = (ImageView) findViewById(R.id.iv_id);
        tv_id = (TextView) findViewById(R.id.tv_id);
        rel_phone = (RelativeLayout) findViewById(R.id.rel_phone);
        iv_phone = (ImageView) findViewById(R.id.iv_phone);
        tv_phone = (TextView) findViewById(R.id.tv_phone);
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_personal_info);
        bindViews();
        ttb_title_bar.findViewById(R.id.tvNavBack).setOnClickListener(this);
        rel_name.setOnClickListener(this);
        rel_phone.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        getUserInfo();
    }

    private void getUserInfo() {
        mDialog.show();
        PersonalNetWork.getUserInfo(UserTokenUtil.getToken(PersonalInfoActivity.this), new CallBackListener() {
            @Override
            public void onCallBackCompleted(Msg msg) {
                mDialog.cancel();
                if (msg != null && msg.getSuccess() && msg.getData() != null && msg.getData() instanceof UserEntity) {
                    mUser = (UserEntity) msg.getData();
                    if (mUser != null) {
                        tv_name.setText(mUser.getName());
                        if (!TextUtils.isEmpty(mUser.getIdNumber())) {
                            if (mUser.getIdNumber().length() >= 4) {
                                tv_id.setText("**************" + mUser.getIdNumber().substring(mUser.getIdNumber().length() - 4));
                            } else {
                                tv_id.setText(mUser.getIdNumber());
                            }
                        }
                        if (!TextUtils.isEmpty(mUser.getPhone())) {
                            tv_phone.setText(mUser.getPhone());
                        }
                    }
                }
            }

            @Override
            public void onCallBackError(Msg msg) {
                mDialog.cancel();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvNavBack:
                back();
                break;
            case R.id.rel_name:
                EditTextDialog dialog = new EditTextDialog(this, "修改姓名", mUser == null ? "" : mUser.getName(), getResources().getDrawable(R.drawable.kaihu_name), "请输入姓名", new EditTextDialog.OnSureClickListener() {
                    @Override
                    public void onSureClick(final String txt) {
                        mDialog.show();
                        JSONObject object = new JSONObject();
                        try {
                            object.put("Name", txt);
                            PersonalNetWork.alertUserInfo(UserTokenUtil.getToken(PersonalInfoActivity.this), object.toString(), new CallBackListener() {
                                @Override
                                public void onCallBackCompleted(Msg msg) {
                                    mDialog.cancel();
                                    if (msg != null && msg.getSuccess()) {
                                        tv_name.setText(txt);
                                        ToastUtils.showShortToast(PersonalInfoActivity.this, "修改成功");
                                    } else {
                                        if (TextUtils.isEmpty(msg.getMsg()) || "null".equals(msg.getMsg())) {
                                            ToastUtils.showShortToast(PersonalInfoActivity.this, "修改失败");
                                        } else {
                                            ToastUtils.showShortToast(PersonalInfoActivity.this, msg.getMsg());
                                        }
                                    }
                                }

                                @Override
                                public void onCallBackError(Msg msg) {
                                    mDialog.cancel();
                                }
                            });
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                dialog.show();
                break;
            case R.id.rel_phone:
                Intent intent = new Intent(this, AlertPhoneActivity.class);
                startActivityForResult(intent, 99);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 99 && resultCode == 998) {
            String newPhone = data.getStringExtra("phone");
            if (newPhone.length() >= 4) {
                tv_phone.setText("*******" + newPhone.substring(newPhone.length() - 4));
            } else {
                tv_phone.setText(newPhone);
            }
        }
    }
}
